package com.atguigu.srb.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.BucketStat;
import com.aliyun.oss.model.CannedAccessControlList;

import java.io.File;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        OSS ossClient = new OSSClientBuilder().build("oss-cn-shenzhen.aliyuncs.com","LTAI5tBnqSRdeqREx6r1MMvH","vSGPYtuk38NiqIjQn6kLZYzYmFTRtq");
        ossClient.putObject("srb","a/1.jpg",new File("d:/131809s2b44b86hm39b2m4.jpg"),null);
        ossClient.shutdown();
    }

    public void isExist(){
        OSS ossClient = new OSSClientBuilder().build("oss-cn-shenzhen.aliyuncs.com","LTAI5tBnqSRdeqREx6r1MMvH","vSGPYtuk38NiqIjQn6kLZYzYmFTRtq");
        boolean b = ossClient.doesBucketExist("srb");
        System.out.println(b);
    }

    public void acl(){

        OSS ossClient = new OSSClientBuilder().build("oss-cn-shenzhen.aliyuncs.com","LTAI5tBnqSRdeqREx6r1MMvH","vSGPYtuk38NiqIjQn6kLZYzYmFTRtq");
        ossClient.setBucketAcl("srb", CannedAccessControlList.PublicRead);

    }

    public void create(){
        OSS ossClient = new OSSClientBuilder().build("oss-cn-shenzhen.aliyuncs.com","LTAI5tBnqSRdeqREx6r1MMvH","vSGPYtuk38NiqIjQn6kLZYzYmFTRtq");
        ossClient.createBucket("srb");
        ossClient.shutdown();
    }

    public void delete(){
        OSS ossClient = new OSSClientBuilder().build("oss-cn-shenzhen.aliyuncs.com","LTAI5tBnqSRdeqREx6r1MMvH","vSGPYtuk38NiqIjQn6kLZYzYmFTRtq");
        List<Bucket> buckets = ossClient.listBuckets();
        for (Bucket bucket : buckets) {
            String region = bucket.getRegion();
            System.out.println(region);
            if(region.equals("cn-shenzhen")){
                BucketStat bucketStat = ossClient.getBucketStat(bucket.getName());
                Long objectCount = bucketStat.getObjectCount();
                System.out.println(objectCount);
                if(objectCount==0){
                    ossClient.deleteBucket(bucket.getName());
                }
            }
        }
        ossClient.shutdown();
    }
}
